﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using QDT.CFCA.Seals;
using QDT.Common;

namespace QDT.CFCA
{
    public class PDFSealFunction
    {
        public Byte[] SealPdf(Byte[] pdf, Seal seal)
        {
            var strategy = @"<Request>
                        <Type>3</Type>
                        <SealCode>{1}</SealCode>
                        <SealPassword>{2}</SealPassword>
                        <KeyPassword>{3}</KeyPassword>
                        <Keyword>{0}</Keyword>
                    </Request>".FormatWith(seal.SealKeyword, seal.SealCode(), seal.SealPassword(), seal.SealPassword());
            var service = SealServiceBuilder.GetPdfSeal();
            return service.sealAutoPdfB(pdf, strategy);
        }

        public void ConvertWebToPdf(int projectId, string savePath)
        {
            var url = BaseConfig.AgreeUri + projectId;
            var p = Process.Start(BaseConfig.ProcessUri, @"{0} {1}".FormatWith(url, savePath));
            if (p != null) p.WaitForExit();
        }

        public Byte[] SealByProjectAgency(Byte[] pdf, int? agency)
        {
            Byte[] result;
            var seal = new QdtSeal();
            var file = SealPdf(pdf, seal);

            switch (agency)
            {
                default:
                    result = file;
                    break;
                case 0:
                    result = file;
                    break;
                case 1:
                    result = file;
                    break;
                case 2:
                    var jdhSeal = new JdhSeal();
                    var jdhFile = SealPdf(file, jdhSeal);
                    result = jdhFile;
                    break;
                case 4:
                    var zyctSeal = new ZyctSeal();
                    var zyctFile = SealPdf(file, zyctSeal);
                    result = zyctFile;
                    break;
                case 5:
                    var dsxSeal = new DsxSeal();
                    var dsxFile = SealPdf(file, dsxSeal);
                    result = dsxFile;
                    break;
            }
            return result;
        }


    }
}
